package com.Questions.BinarySearch;

public class leetcode367 {
//    public boolean isPerfectSquare(int num) {
//        if(num==2147483647){
//            return false;
//        }
//        int low=0;
//        int high=num;
//        while (high>=low){
//            int mid=low+(high-low)/2;
//            if(mid*mid>num){
//                high=mid-1;
//            }else if(mid*mid<num){
//                low=mid+1;
//            }else{
//                return true;
//            }
//        }
//        return false;
//    }
    public boolean isPerfectSquare(int num) {
        int left = 0, right = num;
        while (left <= right) {
            int mid = (right - left) / 2 + left;
            long square = (long) mid * mid;
            if (square < num) {
                left = mid + 1;
            } else if (square > num) {
                right = mid - 1;
            } else {
                return true;
            }
        }
        return false;
    }
}
